package com.cy.pj.projects.service.impl;

import com.cy.pj.projects.pojo.DetailProjectVO;
import com.cy.pj.projects.service.DetailProjectService;
import com.cy.pj.projects.dao.DetailProjectDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 业务逻辑层;
 * */
@Service
public class DetailProjectServiceImpl implements DetailProjectService {
    @Autowired
    private DetailProjectDao detailProjectDao;

    //1.基于id查询t_project表数据,显示项目详情;
    @Override
    public DetailProjectVO getDetailProjectObjects(Integer projectId) {
        //1.基于id查询t_project表数据;
        DetailProjectVO detailProjectObjects = detailProjectDao.getDetailProjectObjects(projectId);
        //2.1.获取 status 值;
        Integer status = detailProjectObjects.getStatus();
        //2.2.根据 (审核值)status 确定 (对应的审核状态)statusText;
        switch (status) {
            case 0:
                detailProjectObjects.setStatusText("审核中");
                break;
            case 1:
                detailProjectObjects.setStatusText("众筹中");
                break;
            case 2:
                detailProjectObjects.setStatusText("众筹成功");
                break;
            case 3:
                detailProjectObjects.setStatusText("众筹失败");
                break;
            default:
                break;
        }

        //3.1.获取 deployDate 值,根据 (项目发起时间)deployDate 计算 (剩余天数)lastDay;    ---- 2020-10-15 ;
        String deployDate = detailProjectObjects.getDeployDate();
        //3.2.获取当前日期;
        Date currentDay = new Date();
        //3.3.把众筹日期解析成 Date 类型;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //1.把众筹日期解析成 Date 类型;
            Date deployDay = format.parse(deployDate);
            //2.获取当前当前日期的时间戳;
            long currentTimeStamp = currentDay.getTime();
            //3.获取众筹日期的时间戳;
            long deployTimeStamp = deployDay.getTime();
            //4.两个时间戳相减计算当前已经过去的时间;
            long pastDays = (currentTimeStamp - deployTimeStamp) / 1000 / 60 / 60 / 24;
            //5.获取总的众筹天数;
            Integer totalDays = detailProjectObjects.getDay();
            //6.使用总的众筹天数,减去已经过去的天数,得到剩余天数;
            Integer lastDay = (int) (totalDays - pastDays);
            //7.将获得的lastDay封装给detailProjectObjects对象;
            detailProjectObjects.setLastDay(lastDay);


        } catch (ParseException e) {
            e.printStackTrace();
        }

        return detailProjectObjects;
    }


}
